<?php
class m_member extends Model{
    var $table = "member",
        $gallery_path,
        $thumb_path;    
    
    function m_member(){
        parent::Model();
        $this->gallery_path = realpath(APPPATH.'../image/foto_member');
        $this->thumb_path = $this->gallery_path.'/thumbs';
    }
    function getCountRows($params){
        if($params['nama'] != NULL){
            $action = "and m.nama like ('%$params[nama]%')";
        }
        $query = $this->db->query("select m.id,m.nama as member,m.tgl_lahir,m.jenis_kelamin,dm.alamat,dm.no_telp,p.nama as pekerjaan
                         from member m join dinamis_member dm on dm.id_member = m.id left join pekerjaan p on dm.id_pekerjaan = p.id where dm.status='1' $action");
        
        return $query->num_rows();
    }
    function getListMember($offset = NULL,$limit = NULL,$params,$sortBy,$type){
        if($params['nama'] != NULL){
            $action = "and m.nama like ('%$params[nama]%')";
        }
        if($sortBy == 'nama'){
            $order = "order by m.nama $type";
        }else $order = 'order by m.nama asc';
        $query = $this->db->query("select m.id,m.nama as member,m.tgl_lahir,m.jenis_kelamin,dm.alamat,dm.no_telp,p.nama as pekerjaan
                         from member m join dinamis_member dm on dm.id_member = m.id left join pekerjaan p on dm.id_pekerjaan = p.id where dm.status='1' $action $order limit $offset,$limit");
        if($query->num_rows() > 0){
            $i = 0;
            foreach ($query->result_array() as $row){
                $result[$i]['member'] = $row;
                $query2 = $this->db->query("select jm.nama as jenis_member,jm.id as id_jenis_member from jenis_keanggotaan jk left join jenis_member jm on jk.id_jenis_member = jm.id where jk.id_member = '$row[id]'");
                if($query2->num_rows > 0){
                    foreach ($query2->result_array() as $row2){
                        $result[$i]['jenis_keanggotaan'][] = $row2;
                    }
                }else {
                    $result[$i]['jenis_kenggotaan'][] = array();
                }
                $i++;
            }
        }
        return $result;
    }
    function getDinamisMember($id = NULL){
        if($id != NULL){
          $where = "where dm.id_member = '$id'";  
        }else $where = "";
        
        $query = $this->db->query('select dm.*,pkj.nama as pekerjaan from dinamis_member dm left join pekerjaan pkj on dm.id_pekerjaan = pkj.id '.$where.' order by status desc');
        if($query->num_rows() > 0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function addDinamisMember($data){
        $dataGambar = $this->uploadValue($this->gallery_path);
        $this->makeThumbnail($dataGambar['full_path'],$dataGambar['image_height'],$dataGambar['image_width'],$this->thumb_path);
        
        $data['gambar'] = $dataGambar['file_name'];
        
        $this->db->where('id_member',$data['id_member']);
        $this->db->update('dinamis_member',array('status' => '0'));
        
        $insert = $this->db->insert('dinamis_member',$data);
        if(!$insert){
            return FALSE;
        }else return TRUE;
    }
    function getMemberById($id = NULL){
        if($id != NULL){
            $where = "and m.id='$id'";
        }else $where = "";
        $query = $this->db->query("select m.id,m.nama as member,p.id as id_pekerjaan,m.tgl_lahir,m.jenis_kelamin,dm.gambar,dm.alamat,dm.no_telp,p.nama as pekerjaan
                         from member m join dinamis_member dm on dm.id_member = m.id left join pekerjaan p on dm.id_pekerjaan = p.id where dm.status='1' $where");
        if($query->num_rows() > 0){
            $i = 0;
            foreach ($query->result_array() as $row){
                $result[$i]['member'] = $row;
                $query2 = $this->db->query("select jm.nama as jenis_member,jm.id as id_jenis_member,jk.id as id_jenis_keanggotaan from jenis_keanggotaan jk left join jenis_member jm on jk.id_jenis_member = jm.id where jk.id_member = '$row[id]'");
                if($query2->num_rows > 0){
                    foreach ($query2->result_array() as $row2){
                        $result[$i]['jenis_keanggotaan'][] = $row2;
                    }
                }else {
                    $result[$i]['jenis_kenggotaan'][] = array();
                }
                $i++;
            }
        }
        return $result;
    }
    function addMember($data){
        $nama = $data['nama'];
        $tgLahir = date2mysql($data['tglLahir']);
        $jenKel = $data['jeKel'];
        $alamat = $data['alamat'];
        $noTelp = $data['noTelp'];
        $pekerjaan = $data['pekerjaan'];
        $jenisMember = $data['jenisMember'];
        $tanggal = date('Y-m-d');
        $dataGambar = $this->uploadValue($this->gallery_path);
        $this->makeThumbnail($dataGambar['full_path'],$dataGambar['image_height'],$dataGambar['image_width'],$this->thumb_path);
        
        $member = array('nama' => $nama,
            'tgl_lahir' => $tgLahir,
            'jenis_kelamin' => $jenKel);
        $this->db->insert($this->table,$member);
        $id = $this->db->insert_id();
        
        $dinamisMember = array('tanggal' => $tanggal,
            'id_member' => $id,
            'alamat' => $alamat,
            'no_telp' => $noTelp,
            'id_pekerjaan' => $pekerjaan,
            'gambar' => $dataGambar['file_name'],
            'status' => '1');
        $insert = $this->db->insert('dinamis_member',$dinamisMember);
        foreach ($jenisMember as $row){
            if($row != ""){
                $jenisKeanggotaan = array('id_member' => $id,
                    'id_jenis_member' => $row);
                $this->db->insert('jenis_keanggotaan',$jenisKeanggotaan);
            }
        }
        if(!$insert){
            return FALSE;
        }else return TRUE;
    }
    function editMember($data){
        $idMember = $data['idMember'];
        $nama = $data['nama'];
        $tglLahir = date2mysql($data['tglLahir']);
        $jeKel = $data['jeKel'];
        $alamat = $data['alamat'];
        $noTelp = $data['noTelp'];
        $pekerjaan = $data['pekerjaan'];
        $jenisMember = $data['jenisMember'];
        $dataGambar = $this->uploadValue($this->gallery_path);
        $this->makeThumbnail($dataGambar['full_path'],$dataGambar['image_height'],$dataGambar['image_width'],$this->thumb_path);
        
        $member = array('nama' => $nama,
            'tgl_lahir' => $tglLahir,
            'jenis_kelamin' => $jeKel);
        $this->db->where('id',$idMember);
        $this->db->update($this->table,$member);
        
        if($dataGambar['file_name'] != ''){
            $dinamisMember = array('alamat' => $alamat,
            'no_telp' => $noTelp,
            'id_pekerjaan' => $pekerjaan,
            'gambar' => $dataGambar['file_name']);
        }else{
         $dinamisMember = array('alamat' => $alamat,
            'no_telp' => $noTelp,
            'id_pekerjaan' => $pekerjaan);
        }
        $this->db->where('id_member',$idMember);
        $this->db->where('status','1');
        $update = $this->db->update('dinamis_member',$dinamisMember);
        
        foreach ($jenisMember as $row){
            if($row != ""){
                $jenisKeanggotaan = array('id_member' => $idMember,
                    'id_jenis_member' => $row);
                $this->db->insert('jenis_keanggotaan',$jenisKeanggotaan);
            }
        }
        if(!$update){
            return FALSE;
        }else return TRUE;
    }
    function hapusKeanggotaan($id){
        $delete = $this->db->delete('jenis_keanggotaan',array('id'=>$id));
        if($delete){
            $status = true;
        }else $status = false;
        
        return $status;
    }
    function getKeanggotaanById($id){
        $query = $this->db->query("select j.nama as jenis_member from jenis_keanggotaan jk join jenis_member j on jk.id_jenis_member = j.id where jk.id_member = '$id'");
        if($query->num_rows()>0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function deleteMember($id){
        $delete = $this->db->delete($this->table,array('id'=>$id));
        if(!$delete){
            return FALSE;
        }else return TRUE;
    }
    function searchMemberUser($data){
        $query = $this->db->query("select m.id,m.nama,dm.alamat from member m join dinamis_member dm on dm.id_member = m.id where locate('$data',m.nama) > 0 and m.id not in (select id_member from user) order by locate ('$data',m.nama)");
        if($query->num_rows() > 0){
           $result = $query->result_array();    
        }else $result = array();
        
        return $result;
    }
    function cekDonaturTunggal($idMember){
        $query = $this->db->query("select count(*) as jumlah from jenis_keanggotaan where id_member = '$idMember' and id_jenis_member = '3'");
        return $query->result_array();
    }
    function cekKakakPembina($idMember){
        $query = $this->db->query("select count(*) as jumlah from jenis_keanggotaan where id_member = '$idMember' and id_jenis_member = '4'");
        return $query->result_array();
    }
    /*
     * mengembalikan data file yg diupload
     */
    function uploadValue($gallery_path){
        $config['upload_path'] = $this->gallery_path;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']	= '2000';
        
        $this->upload->initialize($config);
        $this->upload->do_upload();
        
        return $dataGambar = $this->upload->data();
    }
    /*
     * membuat gambar thumbnail pada file yg diupload
     */
    function makeThumbnail($fullPath,$imageHeight,$imageWidth,$thumbPath){
       $widthThumb = 150;
       $heightThumb = ceil(($widthThumb * $imageHeight)/$imageWidth);
       
       $config = array(
       'source_image'=> $fullPath,
       'new_image'=> $thumbPath,
       'maintain_ration'=>true,
       'width'=>$widthThumb,
       'height'=>$heightThumb
       );

       $this->load->library('image_lib', $config);
       $this->image_lib->resize();
    }
}
?>
